package com.example.xwuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xwcommon.entity.User;
import com.example.xwcommon.service.UserService;
import com.example.xwcommon.vo.Response;
import com.example.xwuser.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public Response<User> reg(String nickName, String loginName, String password, String photo) {
        User user = new User();
        user.setNickname(nickName);
        user.setLoginName(loginName);
        user.setUserPassword(password);
        user.setPhoto(photo);
        user.setScore(50);
        user.setAttentionCount(0);
        user.setState(1);
        userMapper.insert(user);
        return new Response<>(200, "注册成功", user);
    }

    @Override
    public Response<User> login(String loginName, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_loginname", loginName);
        if(userMapper.selectOne(queryWrapper)==null){
            return new Response<>(401, "用户名不存在，请检查输入的用户名", null);
        }
        queryWrapper.eq("user_loginpwd", password);
        User user=userMapper.selectOne(queryWrapper);
        if(user!=null){
            return new Response<>(200, "登录成功", user);
        }
        return new Response<>(402, "密码错误，请检查输入的密码", null);
    }

    @Override
    @Transactional
    public Response<Integer> addScore(String loginName, Integer score) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_loginname", loginName);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            user.setScore(user.getScore()+score);
            userMapper.updateById(user);
            return new Response<>(200, "积分增加成功", user.getScore());
        }
        return new Response<>(401, "用户名不存在，请检查输入的用户名", null);
    }

    @Override
    @Transactional
    public Response<User> updateState(String loginName, Integer state) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_loginname", loginName);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            user.setState(state);
            userMapper.updateById(user);
            return new Response<>(200, "用户状态修改成功", user);
        }
        return new Response<>(401, "用户名不存在，请检查输入的用户名", null);
    }

    @Override
    public Response<User> getUserByLoginName(String loginName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_loginname", loginName);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return new Response<>(200, "查询成功", user);
        }
        return new Response<>(401, "用户名不存在，请检查输入的用户名", null);
    }

    @Override
    public Response<User> getUserById(Integer userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return new Response<>(200, "查询成功", user);
        }
        return new Response<>(401, "用户名不存在，请检查输入的用户名", null);
    }


    @Override
    public Response<User> updateAttentionCount(String loginName, Integer attentionCount) {
        User user = getUserByLoginName(loginName).getData();
        user.setAttentionCount(user.getAttentionCount()+attentionCount);
        userMapper.updateById(user);
        if(user!=null){
            return new Response<>(200, "关注数增加成功", user);
        }
        return new Response<>(401, "用户名不存在，请检查输入的用户名", null);
    }

    @Override
    public Response<List<User>> getAllUser() {
        return new Response<>(200, "查询成功", userMapper.selectList(null));
    }

}
